package com.qzone;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LocationList extends Activity {

	TextView schoolname, studentname, regnumber;
	static Context mContext;
	static ListView locationlist;
	static String stdname, schname, regno,masterid,sipnumber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_location_list);
		schoolname = (TextView) findViewById(R.id.schoolname);
		studentname = (TextView) findViewById(R.id.studentname);
		regnumber = (TextView) findViewById(R.id.regnumber);

		stdname = getIntent().getStringExtra("STUDENTNAME");
		regno = getIntent().getStringExtra("REGNUMBER");
		schname = getIntent().getStringExtra("SCHOOLNAME");
		masterid = getIntent().getStringExtra("MASTERID");
		sipnumber=getIntent().getStringExtra("SIPNUMBER");
		
		studentname.setText(stdname);
		regnumber.setText(regno);
		schoolname.setText(schname);

		getLocation();

		locationlist = (ListView) findViewById(R.id.locationlist);

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent intent=new Intent(mContext,StudentList.class);
		startActivity(intent);
    	finish();
		super.onBackPressed();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.location_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_delete) {
			CommonUtil.deleteStudent(mContext, masterid);
			return true;
		}
		else if (id == android.R.id.home)
		{
			onBackPressed();
		}
		else if(id == R.id.action_refresh)
		{
			getLocation();	
		}
		return super.onOptionsItemSelected(item);
	}

	public static void locationRes(String res) {

		if (res != null) {
			try {
				JSONObject jsonObj = new JSONObject(res);
				if (jsonObj.get("status").equals("2")) {
					Toast.makeText(mContext,
							jsonObj.get("error_message").toString(),
							Toast.LENGTH_LONG).show();
				} else if (jsonObj.get("status").equals("1")) {
					final JSONArray jsonArr = jsonObj
							.getJSONArray("log_details");
					CustomAdapter customAdapter = new CustomAdapter(mContext,
							jsonArr);
					locationlist.setAdapter(customAdapter);
					locationlist
							.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> arg0,
										View arg1, int arg2, long arg3) {

									try {
										JSONObject jsonloc = jsonArr.getJSONObject(arg2);
										Intent intent = new Intent(mContext,StudentMap.class);
										intent.putExtra("STUDENTNAME", stdname);
										intent.putExtra("REGNUMBER", regno);
										intent.putExtra("SCHOOLNAME", schname);
										intent.putExtra("TAGNUMBER", jsonloc.getString("tag_number"));
										intent.putExtra("LATITUDE", jsonloc.getString("latitude"));
										intent.putExtra("LONGITUDE", jsonloc.getString("longitude"));
										intent.putExtra("BUSNO", jsonloc.getString("busno"));

										mContext.startActivity(intent);
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}

								}

							});

				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			Log.e("ServiceHandler", "Couldn't get any data from the url");
		}

	}

	static class CustomAdapter extends BaseAdapter {
		Context mContext;
		JSONArray jsonr;
		private static LayoutInflater inflater = null;

		CustomAdapter(Context c, JSONArray jsr) {
			mContext = c;
			jsonr = jsr;
			inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return jsonr.length();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			View vi = arg1;
			if (arg1 == null) {
				vi = inflater.inflate(R.layout.loclist, null);
			}

			try {
				JSONObject jsonloc = jsonr.getJSONObject(arg0);
				TextView a1 = (TextView) vi.findViewById(R.id.a1);
				TextView a2 = (TextView) vi.findViewById(R.id.a2);
				TextView a3 = (TextView) vi.findViewById(R.id.a3);
				TextView a4 = (TextView) vi.findViewById(R.id.a4);
				
				if(jsonloc.getString("a1")!=null)
					a1.setText(jsonloc.getString("a1"));
				else
					a1.setVisibility(View.GONE);
				if(jsonloc.getString("a2")!=null)
					a2.setText(jsonloc.getString("a2"));
				else
					a2.setVisibility(View.GONE);
				if(jsonloc.getString("a3")!=null)
					a3.setText(jsonloc.getString("a3"));
				else
					a3.setVisibility(View.GONE);
				if(jsonloc.getString("a4")!=null)
					a4.setText(jsonloc.getString("a4"));
				else
					a4.setVisibility(View.GONE);
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return vi;
		}

	}
	
	private void getLocation(){
		AsynHttp asynhttp = new AsynHttp(mContext, sipnumber,"tagno="
				+ getIntent().getStringExtra("TAGNO"));
		asynhttp.execute(AsynHttp.STDLOG);		
	}

}
